# -*- coding: utf-8 -*-

# File Name： ocr_module_test
# Description :
# Author : lirui
# create_date： 2022/8/15
# Change Activity:
import requests

import ml
from ml import logger

test_image_dir = rf"D:\image\test_img"
server_address = rf'http://127.0.0.1:8080/'
ocr_test_config = [
    dict(
        module_name='箱顶洞识别',
        image_dir=rf"{test_image_dir}/箱顶",
        url=rf'{server_address}ocr/hole_detect',
    ),
    dict(
        module_name='桥吊箱号识别',
        image_dir=rf"{test_image_dir}/箱号",
        url=rf'{server_address}ocr/check',
    ),
    dict(
        module_name='箱门侧识别',
        image_dir=rf"{test_image_dir}/箱门",
        url=rf'{server_address}door_side_detection',
    ),
    dict(
        module_name='危险品标志识别',
        image_dir=rf"{test_image_dir}/危险品",
        url=rf'{server_address}ocr/dangerous',
    ),
    dict(
        module_name='桥吊箱号识别',
        image_dir=rf"{test_image_dir}/箱号",
        url=rf'{server_address}ocr/check',
    ),

    dict(
        module_name='铅封识别',
        image_dir=rf"{test_image_dir}/铅封",
        url=rf'{server_address}ocr/seal_detect',
    ),
    dict(
        module_name='箱顶洞识别',
        image_dir=rf"{test_image_dir}/箱顶",
        url=rf'{server_address}ocr/hole_detect',
    )
]


def _print_results(info):
    url = info['url']
    img_dir = info['image_dir']
    img_path_lst = ml.list_file_paths_in_dir(img_dir)
    logger.info(rf'待测图片共计{len(img_path_lst)}张')
    for i, path in enumerate(img_path_lst):
        file = {'pic': open(path, 'rb')}
        res = requests.post(url=url, files=file, data={'is_return_image': 'true'})
        print(res.text)
        print(rf"当前第{i}张图片，识别结果：{res.text}")


def _main():
    logger.info('开始测试识别服务')
    for index, info in enumerate(ocr_test_config):
        logger.info(rf"进行第{index}步测试，测试名称：{info['module_name']}，测试图片目录：{info['image_dir']}，测试url：{info['url']}")
        _print_results(info)


if __name__ == '__main__':
    _main()
